package com.md.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.md.base.BaseRepositoryImpl;
import com.md.config.database.CustomMapper;
import com.md.domain.model.bo.QueryRoleBo;
import com.md.domain.model.po.Role;
import com.md.repository.RoleRepository;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author 刘骄阳
 * 2022-06-12 1:22
 */
@Repository
public class RoleRepositoryImpl extends BaseRepositoryImpl<Role> implements RoleRepository {
  public RoleRepositoryImpl(CustomMapper<Role> mapper) {
    super(mapper);
  }

  @Override
  public List<Role> queryRoleListByParentIds(final List<Long> parentIds) {
    if (ObjectUtils.isEmpty(parentIds)) {
      return Lists.newArrayList();
    }
    LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
    wrapper.in(Role::getParentId, parentIds);
    return getMapper().selectList(wrapper);
  }

  @Override
  public IPage<Role> queryPage(final QueryRoleBo queryRoleBo) {
    LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
    // 父id
    wrapper.eq(!(ObjectUtils.isEmpty(queryRoleBo.getParentId())), Role::getParentId, queryRoleBo.getParentId())
      // 根id
      .eq(!(ObjectUtils.isEmpty(queryRoleBo.getRootId())), Role::getRootId, queryRoleBo.getRootId())
      // 角色名
      .like(!(ObjectUtils.isEmpty(queryRoleBo.getName())), Role::getName, queryRoleBo.getName())
      // 大于等于开始时间
      .ge(!(ObjectUtils.isEmpty(queryRoleBo.getStartTime())), Role::getCreateTime, queryRoleBo.getStartTime())
      // 小于等于结束时间
      .le(!(ObjectUtils.isEmpty(queryRoleBo.getEndTime())), Role::getCreateTime, queryRoleBo.getEndTime());
    return getMapper().selectPage(new Page<>(queryRoleBo.getPageNum(), queryRoleBo.getPageSize()), wrapper);
  }
}
